/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-12-16     RT-Thread    first version
 * 2025-05-12     Claude       add uart transmission
 * 2025-05-12     Claude       fix garbled output
 * 2025-05-13     Trae AI      optimize for Python receiver
 * 2025-05-14     Trae AI      add RTOS features
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
//-------------软件包-------------------
#include "mpu6xxx.h"
#include <stdio.h>  // 用于sprintf函数
#include "thread_manage.h"  // 添加线程管理头文件

#define MPU6050_I2C_BUS_NAME          "i2c1"  /* 传感器连接的I2C总线设备名称 */
#define UART_DEVICE_NAME              "uart1"  /* 串口设备名称 */
#define WINDOW_SIZE                   128     /* 数据窗口大小，与Python接收端匹配 */
#define SAMPLE_INTERVAL               50      /* 采样间隔(ms) */

/* 初始化全局变量 */
static rt_device_t uart_dev = RT_NULL;  // 串口设备句柄
static struct rt_thread event_handler_thread;  // 事件处理线程
static rt_uint8_t event_handler_stack[1024];  // 事件处理线程栈

/* 初始化串口设备 */
static rt_err_t uart_init(void)
{
    rt_err_t ret = RT_EOK;
    
    /* 查找串口设备 */
    uart_dev = rt_device_find(UART_DEVICE_NAME);
    if (uart_dev == RT_NULL)
    {
        return -RT_ERROR;
    }
    
    /* 以读写方式打开串口设备 */
    ret = rt_device_open(uart_dev, RT_DEVICE_OFLAG_RDWR);
    if (ret != RT_EOK)
    {
        return -RT_ERROR;
    }
    
    /* 配置串口参数 */
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  // 使用默认配置
    config.baud_rate = BAUD_RATE_115200;  // 设置波特率为115200
    config.data_bits = DATA_BITS_8;       // 8位数据位
    config.stop_bits = STOP_BITS_1;       // 1位停止位
    config.parity    = PARITY_NONE;       // 无校验
    
    /* 控制串口设备配置 */
    ret = rt_device_control(uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
    if (ret != RT_EOK)
    {
        rt_device_close(uart_dev);
        return -RT_ERROR;
    }
    
    /* 等待串口稳定 */
    rt_thread_mdelay(100);
    
    /* 清空接收缓冲区 */
    uint8_t temp;
    while (rt_device_read(uart_dev, 0, &temp, 1) > 0);
    
    return RT_EOK;
}

/* 发送传感器数据到串口 */
static rt_err_t send_sensor_data(struct mpu6xxx_3axes *accel, struct mpu6xxx_3axes *gyro)
{
    char data_buffer[128];  // 增大缓冲区以容纳浮点数格式
    rt_size_t len;
    
    /* 将整数数据转换为浮点数格式 */
    float acc_x = accel->x / 16384.0f;  // 根据MPU6050的量程±2g转换为g值
    float acc_y = accel->y / 16384.0f;  // 16384 LSB/g是±2g量程下的灵敏度
    float acc_z = accel->z / 16384.0f;
    
    float gyro_x = gyro->x / 131.0f;    // 根据MPU6050的量程±250°/s转换为°/s
    float gyro_y = gyro->y / 131.0f;    // 131 LSB/(°/s)是±250°/s量程下的灵敏度
    float gyro_z = gyro->z / 131.0f;
    
    /* 格式化数据为浮点数格式: "acc_x,acc_y,acc_z,gyro_x,gyro_y,gyro_z" */
    len = rt_snprintf(data_buffer, sizeof(data_buffer), "%.4f,%.4f,%.4f,%.4f,%.4f,%.4f\r\n", 
                     acc_x, acc_y, acc_z, 
                     gyro_x, gyro_y, gyro_z);
    
    /* 通过串口发送数据 */
    rt_size_t send_size = rt_device_write(uart_dev, 0, data_buffer, len);
    if (send_size != len)
    {
        return -RT_ERROR;
    }
    
    return RT_EOK;
}

/* 事件处理线程入口函数 */
static void event_handler_entry(void *parameter)
{
    while (1)
    {
        /* 处理事件 */
        har_event_handler();
    }
}

int main(void)
{
    struct mpu6xxx_device *dev;  // 定义mpu6xxx设备结构体指针
    rt_err_t ret;
    
    /* 先初始化串口 */
    ret = uart_init();
    if (ret != RT_EOK)
    {
        return -1;
    }
    
    /* 发送测试消息 */
    rt_device_write(uart_dev, 0, "HAR系统启动，串口测试消息\r\n", 28);
    rt_thread_mdelay(100);  // 短暂延时确保消息发送完成
    
    /* 初始化mpu6xxx设备 */
    dev = mpu6xxx_init(MPU6050_I2C_BUS_NAME, RT_NULL);
    if (dev == RT_NULL)
    {
        return -1;
    }
    
    /* 初始化线程和通信机制 */
    ret = har_thread_init(uart_dev, dev);
    if (ret != RT_EOK)
    {
        return -1;
    }
    
    /* 初始化事件处理线程 */
    ret = rt_thread_init(&event_handler_thread, 
                        "event", 
                        event_handler_entry, 
                        RT_NULL, 
                        event_handler_stack, 
                        sizeof(event_handler_stack), 
                        12, 
                        10);
    if (ret != RT_EOK)
    {
        return -1;
    }
    
    /* 启动事件处理线程 */
    rt_thread_startup(&event_handler_thread);
    
    return RT_EOK;
}